﻿Imports System.Data
Imports System.Data.SqlClient
Partial Class update
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents xoa As System.Web.UI.WebControls.CheckBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not Page.IsPostBack Then
            Session("Back") = Request.UrlReferrer.ToString
            Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
            Dim sSql As String = "select iddonvi,tendonvi from donvi order by ttbaocao"
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(sSql, myConn)
            Dim myDataSet As New DataSet
            myAdapter.Fill(myDataSet, sSql)
            cboDonvi.DataSource = myDataSet
            cboDonvi.DataBind()
            sSql = "select idlienhe,ttlienhe from lienhe  order by ttbaocao"
            myAdapter = New SqlDataAdapter(sSql, myConn)
            Dim myDataSet2 As New DataSet
            myAdapter.Fill(myDataSet2, sSql)
            cboLienhe.DataSource = myDataSet2
            cboLienhe.DataBind()
            myConn.Close()
            If IsNumeric(Request.QueryString("id")) Then
                BindGrid(Request.QueryString("id"))
            Else
                Response.Redirect("order.aspx")
            End If

        End If

    End Sub


    Private Sub BindGrid(ByVal vId As Int16)
        On Error Resume Next
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String = "select top 1 iddonhang,xoa, iddonvi, dh.idlienhe ,convert(varchar(10),ngaygiao,103) as ngaygiao,(tongtien- giam) as conlai,doanhthu,ghichu from donhang dh  where  iddonhang = " & vId
        myConn.Open()
        Dim myComm As New SqlCommand(sSql, myConn)
        Dim myDr As SqlDataReader
        myDr = myComm.ExecuteReader
        If myDr.Read Then
            cboDonvi.SelectedValue = myDr("iddonvi")
            cboLienhe.SelectedValue = myDr("idlienhe")
            txtngay.Text = myDr("ngaygiao")
            If txtngay.Text = "" Then
                txtngay.Text = VN2SysDate(Now.ToShortDateString)
            End If
            txtDoanhthu.Text = FormatNumber(myDr("doanhthu"), 0)
            If txtDoanhthu.Text = 0 Then
                txtDoanhthu.Text = FormatNumber(myDr("conlai"), 0)
            End If
            txtGhichu.Text = myDr("ghichu")
        End If
        myConn.Close()
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sSql As String
        sSql = "update donhang set" & _
" iddonvi=@iddonvi,idlienhe=@idlienhe,doanhthu=@doanhthu,ngaygiao=@ngaygiao,ghichu=@ghichu,username=@username,ngaycapnhat=getdate(), xoa=@xoa" & _
" where iddonhang=@iddonhang"

        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        MyCommand.Parameters.Add(New SqlParameter("@iddonvi", cboDonvi.SelectedValue))
        MyCommand.Parameters.Add(New SqlParameter("@idlienhe", cboLienhe.SelectedValue))
        MyCommand.Parameters.Add(New SqlParameter("@xoa", False))
        MyCommand.Parameters.Add(New SqlParameter("@doanhthu", IIf(IsNumeric(txtDoanhthu.Text), CDbl(txtDoanhthu.Text), 0)))
        MyCommand.Parameters.Add(New SqlParameter("@ngaygiao", IIf(txtngay.Text = "", "01/01/1900", VN2SysDate(txtngay.Text))))
        MyCommand.Parameters.Add(New SqlParameter("@ghichu", txtGhichu.Text))
        MyCommand.Parameters.Add(New SqlParameter("@iddonhang", Request.QueryString("id")))
        MyCommand.Parameters.Add(New SqlParameter("@username", Seperate(Session("admin"), "username")))
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        Response.Redirect(Session("Back"))
    End Sub

End Class
